var activeButton;
//This method inactivates the submit button
function inactivate(btn) {
	if (btn.className == "active") {
		btn.value = btn.value + "ing...";
		btn.className = "inactive non-clickable";
		activeButton = btn;
	}
}

//this function activates the submit button
function activate() {
	if (activeButton != null && activeButton.className != "active") {
		activeButton.value = activeButton.value.replace("ing...","");
		activeButton.className = "active";
		activeButton = null;
	}
	if (typeof(Recaptcha) != "undefined") Recaptcha.reload();
}

//This method handles post update functions for contact me page
function contactMePostCallback(ajaxRequest) {
	postCallback(ajaxRequest,"1000");
	parent.resizeIframe();
}

//This method does the common form submit ajax callback functions
function postCallback(ajaxRequest,count) {
	var msgSpan = document.getElementById("form-msg");
	msgSpan.innerHTML="";
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		msgSpan.className = "success";
		document.forms[0].reset();
		document.getElementById("char-count").className = "green";
		document.getElementById("char-count").innerHTML = count;
		document.getElementById("char-word").innerHTML = "characters"
	} else msgSpan.className = "error";
	msgSpan.innerHTML = xmlDoc.getElementsByTagName("msg")[0].childNodes[0].nodeValue;
}

//This method handles post update functions for all comments screen and guestbook
function commentPostCallback(ajaxRequest) {
	postCallback(ajaxRequest,"1000");
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	//hide the comment entry section
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") { 
		showHideCommentEntry(document.getElementById("comment-expander"));
	}
	var innerHTML = "<p align='center'><img src='" + webRoot + "/images/ajax-loader-tb.gif' border='0'></p>";
	innerHTML+="<p align='center'>Refreshing comments</p>";
	document.getElementById("comments-list").innerHTML = innerHTML;
	document.getElementById("nav-top").innerHTML = "";
	document.getElementById("nav-bottom").innerHTML = "";
	document.getElementById("nav-top").style.visibility = "hidden";
	document.getElementById("nav-bottom").style.visibility = "hidden";
	Paginator.go(1);
	parent.resizeIframe();
}

//This method changes the running count of messages in the message input box
function checkMessageSize(msgbox,size) {
	//size - 50 = green, size -49 to size-1 = amber, size+ = red
	var charCount = document.getElementById("char-count");
	var charWord = document.getElementById("char-word");
	var len = size - trim(msgbox.value).length
	charCount.innerHTML = len;
	if (len > 49) {
		charCount.className = "green";
		charWord.innerHTML = "characters"
	} else if (len > 0) {
		charCount.className = "amber";
		if (len <= 1) charWord.innerHTML = "character";
		else charWord.innerHTML = "characters";
	} else {
		charCount.className = "red";
		charWord.innerHTML = "character"
	}
}

//This function displays the comments on screen
function commentLoaderCallback(ajaxRequest) {
	var noshow = false;
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
			//display the records
			var section = document.getElementById("comments-list");
			section.innerHTML = "";
			for (var i = 0; i < xmlDoc.getElementsByTagName("comment").length; i++) {
				Comment.print(xmlDoc.getElementsByTagName("comment")[i].childNodes[0].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("comment")[i].childNodes[1].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("comment")[i].childNodes[2].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("comment")[i].childNodes[3].childNodes[0].nodeValue,
						section,true);
			}
			Paginator.setPageTotal(parseInt(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue));
			Paginator.displayNav(resetGenericPagination);
		} else {
			noshow = true;
		}
	} else {
		noshow = true;
	}
	
	if (noshow) {
		document.getElementById("comments-list").innerHTML = "<p align='center'>No comments found.</p>";
		document.getElementById("nav-top").innerHTML = "";
		document.getElementById("nav-bottom").innerHTML = "";
		document.getElementById("nav-top").style.visibility = "hidden";
		document.getElementById("nav-bottom").style.visibility = "hidden";
	}
	parent.resizeIframe();
}

function artifactLoaderCallback(ajaxRequest) {
	var noshow = false;
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
			var section = document.getElementById("artifact-list");
			section.innerHTML = "";
			for (var i = 0; i < xmlDoc.getElementsByTagName("artifact").length; i++) {
				Artifact.print(xmlDoc.getElementsByTagName("artifact")[i].childNodes[0].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("artifact")[i].childNodes[1].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("artifact")[i].childNodes[2].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("artifact")[i].childNodes[3].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("artifact")[i].childNodes[4].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("artifact")[i].childNodes[5].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("artifact")[i].childNodes[6].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("artifact")[i].childNodes[7].childNodes[0].nodeValue,
						section);
			}
			Paginator.setPageTotal(parseInt(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue));
			Paginator.displayNav(resetGenericPagination);
			initLytebox();
		} else {
			noshow = true;
		}
	} else {
		noshow = true;
	}
	
	if (noshow) {
		var artifactType = xmlDoc.getElementsByTagName("artifact-type")[0].childNodes[0].nodeValue;
		var artifactName = "Artifacts";
		if (artifactType =="A") artifactName = "Articles";
		else if (artifactType =="T") artifactName = "Tank Galleries";
		else if (artifactType =="M") artifactName = "Miscellaneous Articles";
		else if (artifactType =="D") artifactName = "DIY Projects";
		else if (artifactType =="R") artifactName = "Product Reviews";
		else if (artifactType =="P") artifactName = "Plants";
		else if (artifactType =="E") artifactName = "Events";
		document.getElementById("artifact-list").innerHTML = "<p align='center'>No " + artifactName + " found.</p>";
		document.getElementById("nav-top").innerHTML = "";
		document.getElementById("nav-bottom").innerHTML = "";
		document.getElementById("nav-top").style.visibility = "hidden";
		document.getElementById("nav-bottom").style.visibility = "hidden";
	}
	parent.resizeIframe();
}

function newsLoaderCallback(ajaxRequest) {
	var noshow = false;
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
			var section = document.getElementById("news-list");
			section.innerHTML = "";
			for (var i = 0; i < xmlDoc.getElementsByTagName("news").length; i++) {
				var divider = true;
				if (i == (xmlDoc.getElementsByTagName("news").length - 1)) divider = false;
				News.print(xmlDoc.getElementsByTagName("news")[i].childNodes[1].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("news")[i].childNodes[2].childNodes[0].nodeValue,
						(xmlDoc.getElementsByTagName("news")[i].childNodes[3].childNodes[0] != undefined) ? xmlDoc.getElementsByTagName("news")[i].childNodes[3].childNodes[0].nodeValue : null,
						xmlDoc.getElementsByTagName("news")[i].childNodes[4].childNodes[0].nodeValue,
						section,divider);
			}
			Paginator.setPageTotal(parseInt(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue));
			Paginator.displayNav(resetNewsPagination);
		} else {
			noshow = true;
		}
	} else {
		noshow = true;
	}
	
	if (noshow) {
		document.getElementById("news-list").innerHTML = "<p align='center'>Not much is happening</p>";
		document.getElementById("news-nav").innerHTML = "";
		document.getElementById("news-nav").style.visibility = "hidden";
	}
}

function artifactStatsCallback(ajaxRequest) {
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		var elem = document.getElementById("artifact-comment-tab");
		elem.innerHTML = xmlDoc.getElementsByTagName("comments")[0].childNodes[0].nodeValue;
		var elem = document.getElementById("visitor-count");
		elem.innerHTML = xmlDoc.getElementsByTagName("visitors")[0].childNodes[0].nodeValue + " view(s)";
	}
}

function calcStatsCallback(ajaxRequest) {
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		var elem = document.getElementById("calc-comment-tab");
		elem.innerHTML = xmlDoc.getElementsByTagName("comments")[0].childNodes[0].nodeValue;
	}
}

function inboxLoaderCallback(ajaxRequest) {
	var noshow = false;
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
			//display the records
			var section = document.getElementById("email-list");
			for (var i = 0; i < xmlDoc.getElementsByTagName("email").length; i++) {
				Email.print(xmlDoc.getElementsByTagName("email")[i].childNodes[0].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("email")[i].childNodes[1].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("email")[i].childNodes[2].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("email")[i].childNodes[3].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("email")[i].childNodes[4].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("email")[i].childNodes[5].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("email")[i].childNodes[6].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("email")[i].childNodes[7].childNodes[0].nodeValue,
						section);
			}
			activate();
			Paginator.setPageTotal(parseInt(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue));
			Paginator.displayNav(resetGenericPagination);
			document.getElementById("email-load-msg").style.display = "none";
		} else {
			noshow = true;
		}
	} else {
		noshow = true;
	}
	
	if (noshow) {
		document.getElementById("email-load-msg").style.display = "inline";
		document.getElementById("email-load-msg").innerHTML = "No emails found";
		document.getElementById("nav-top").innerHTML = "";
		document.getElementById("nav-bottom").innerHTML = "";
		document.getElementById("nav-top").style.visibility = "hidden";
		document.getElementById("nav-bottom").style.visibility = "hidden";
	}
}

function inboxUpdatePostCallback(ajaxRequest) {
	if (document.getElementById("func").value == "D") {
		var xmlDoc = ajaxRequest.responseXML.documentElement;
		if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
			document.getElementById("email-list").innerHTML = "";
			Paginator.go(1);
		} else {
			document.getElementById("form-msg").className = "error";
			document.getElementById("form-msg").innerHTML = xmlDoc.getElementsByTagName("msg")[0].childNodes[0].nodeValue;
		}
	}
}

function newsUpdatePostCallback(ajaxRequest) {
	postCallback(ajaxRequest,"200");
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		var msg = document.getElementById("news-text").value;
   		if (document.getElementById("bookmark").value != "") {
   			msg+= ". " + document.getElementById("bookmark").value;
  		}
		fbPublish(msg,fbPublishCallback)
	}
}

function plantListLoadCallback(ajaxRequest) {
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
			//display the records
			var section = document.getElementById(plantIdList);
			section.innerHTML = "";
			if (newPlant) {
				var option = document.createElement("option");
				option.setAttribute("value", "0");
				option.appendChild(document.createTextNode("New Plant"));
				section.appendChild(option);
			}
			for (var i = 0; i < xmlDoc.getElementsByTagName("plant").length; i++) {
				Plant.print(xmlDoc.getElementsByTagName("plant")[i].childNodes[0].childNodes[0].nodeValue,
						xmlDoc.getElementsByTagName("plant")[i].childNodes[1].childNodes[0].nodeValue,
						section);
			}
			loadedPlants = true;
		}
	}
}


function plantLoadCallback(ajaxRequest) {
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
			//display the records
			var section = document.getElementById("plant-name");
			for (var i = 0; i < xmlDoc.getElementsByTagName("plant").length; i++) {
				document.getElementById("plant-name").value = xmlDoc.getElementsByTagName("plant")[i].childNodes[1].childNodes[0].nodeValue;
				document.getElementById("availability").value = xmlDoc.getElementsByTagName("plant")[i].childNodes[2].childNodes[0].nodeValue;
			}
		}
	}
}

function plantUpdatePostCallback(ajaxRequest) {
	var msgSpan = document.getElementById("form-msg");
	msgSpan.innerHTML="";
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		msgSpan.className = "success";
		document.getElementById("plant-id").selectedIndex = 0;
		document.getElementById("availability").selectedIndex = 0;
		document.getElementById("plant-name").value = "";
		loadedPlants = false;
		loadPlants();
	} else msgSpan.className = "error";
	msgSpan.innerHTML = xmlDoc.getElementsByTagName("msg")[0].childNodes[0].nodeValue;
}

function adminSubsectionLoadCallback(ajaxRequest) {
	document.getElementById(currentSection).innerHTML = ajaxRequest.responseText;
	if (plantIdList != null) loadPlants();
}

function artifactListLoadCallback(ajaxRequest) {
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		//display the records
		var section = document.getElementById("artifact-id");
		section.innerHTML = "";
		var option = document.createElement("option");
		option.setAttribute("value", "0");
		option.appendChild(document.createTextNode("New Artifact"));
		section.appendChild(option);
		for (var i = 0; i < xmlDoc.getElementsByTagName("artifact").length; i++) {
			Artifact.printList(xmlDoc.getElementsByTagName("artifact")[i].childNodes[0].childNodes[0].nodeValue,
					xmlDoc.getElementsByTagName("artifact")[i].childNodes[2].childNodes[0].nodeValue,
					section);
		}
		document.getElementById("artifact-id").disabled = false;
	}
}

function artifactLoadCallback(ajaxRequest) {
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
			//display the records
			for (var i = 0; i < xmlDoc.getElementsByTagName("artifact").length; i++) {
				document.getElementById("artifact-title").value = xmlDoc.getElementsByTagName("artifact")[i].childNodes[2].childNodes[0].nodeValue;
				document.getElementById("description").value = xmlDoc.getElementsByTagName("artifact")[i].childNodes[3].childNodes[0].nodeValue;
				checkMessageSize(document.getElementById("description"),1000)
				if (document.getElementById("artifact-type").value == 'P') {
					document.getElementById("a-plant-id").value = xmlDoc.getElementsByTagName("artifact")[i].childNodes[8].childNodes[0].nodeValue;
				}
			}
		}
	}
}

function artifactUpdateCallback(response) {
	document.getElementById("form-msg").innerHTML = "";
	document.getElementById("artifact").innerHTML = response;
}

function ajaxProxyShowErrMsg(msg) {
	document.getElementById("form-msg").className = "error";
	document.getElementById("form-msg").innerHTML = msg;
	activate();
}

function calculatorPostCallback(ajaxRequest) {
	var xmlDoc = ajaxRequest.responseXML.documentElement;
	if (xmlDoc.getElementsByTagName("update")[0].childNodes[0].nodeValue == "completed") {
		displayCalcResults(xmlDoc);
		parent.resizeIframe();
	} else {
		document.getElementById("form-msg").className = "error";
		document.getElementById("form-msg").innerHTML = xmlDoc.getElementsByTagName("msg")[0].childNodes[0].nodeValue;
	}
}

function displayCalcResults(xmlDoc) {
	var section;
	for (var i = 0; i < xmlDoc.getElementsByTagName("tank-vol")[0].childNodes.length; i++) {
		section = document.getElementById("tank-vol-" + xmlDoc.getElementsByTagName("tank-vol")[0].childNodes[i].nodeName);
		section.innerHTML = xmlDoc.getElementsByTagName("tank-vol")[0].childNodes[i].childNodes[0].nodeValue;
	}

	for (var i = 0; i < xmlDoc.getElementsByTagName("substrate-vol")[0].childNodes.length; i++) {
		section = document.getElementById("substrate-vol-" + xmlDoc.getElementsByTagName("tank-vol")[0].childNodes[i].nodeName);
		section.innerHTML = xmlDoc.getElementsByTagName("substrate-vol")[0].childNodes[i].childNodes[0].nodeValue;
	}
	section = document.getElementById("substrate-quantity-results");
	section.innerHTML = "&nbsp;";
	var quantity = "";
	if (xmlDoc.getElementsByTagName("substrate-quantity").length > 0) {
		quantity += "To fill this substrate you will need any of<br>";
		for (var i = 0; i < xmlDoc.getElementsByTagName("substrate-quantity").length; i++) {
			quantity+= "- ";
			quantity += xmlDoc.getElementsByTagName("substrate-quantity")[i].childNodes[1].childNodes[0].nodeValue;
			quantity += " " + xmlDoc.getElementsByTagName("substrate-quantity")[i].childNodes[1].nodeName
			quantity+= " or ";
			quantity += xmlDoc.getElementsByTagName("substrate-quantity")[i].childNodes[2].childNodes[0].nodeValue;
			quantity += " " + xmlDoc.getElementsByTagName("substrate-quantity")[i].childNodes[2].nodeName
			quantity+= " of ";
			quantity += xmlDoc.getElementsByTagName("substrate-quantity")[i].childNodes[0].childNodes[0].nodeValue;
			quantity+= "<br>";
		}
		quantity+= "- ";
		quantity += xmlDoc.getElementsByTagName("as-vol")[0].childNodes[0].nodeValue;
		quantity += "<br><br>";
		quantity += "<strong>Disclaimer: All the substrate quantities are based on my personal calculations of the substrate weight to volume ratios and has been generalized for a brand/type of substrate. I do not claim any accuracy on the information or contradict any information available from the respective producers of these substrate materials.</strong>"
		section.innerHTML = quantity;
	}

	for (var i = 0; i < xmlDoc.getElementsByTagName("water-vol")[0].childNodes.length; i++) {
		section = document.getElementById("water-vol-" + xmlDoc.getElementsByTagName("tank-vol")[0].childNodes[i].nodeName);
		section.innerHTML = xmlDoc.getElementsByTagName("water-vol")[0].childNodes[i].childNodes[0].nodeValue;
	}
}